# Copyright (c) 2021 Presto Labs Pte. Ltd.
# Author: jingyuan

# Support for any spot exchange so that our pta wont explode

import datetime
from coin.exchange.base.kr_rest.currency import NativeCurrency
from coin.exchange.base.kr_rest.product import NativeProduct


def SpotProduct(exchange):
  def ccy_init(self, ccy, dt=None):
    dt = dt or datetime.datetime.now()
    super(self.__class__, self).__init__(ccy, ccy)

  ccy_class = type(f'{exchange}Currency', (NativeCurrency,), {
      '_exchange': exchange,
      '__init__': ccy_init,
  })

  @classmethod
  def FromStrCurrencyImpl(cls, currency_str, current_datetime):
    return cls(currency_str, current_datetime)

  setattr(ccy_class, 'FromStrCurrencyImpl', FromStrCurrencyImpl)
  setattr(ccy_class, 'FromStrNativeCurrencyImpl', FromStrCurrencyImpl)

  def product_init(self, base, quote):
    super(self.__class__, self).__init__(base, quote,
                                         "%s_%s" % (base.native_symbol, quote.native_symbol))

  product_class = type(f'{exchange}Product', (NativeProduct,), {
      '_exchange': exchange,
      'NativeCurrencyType': ccy_class,
      '__init__': product_init,
  })

  @classmethod
  def FromStrNativeProductImpl(cls, product, current_datetime):
    assert isinstance(product, str), type(product)
    base, quote = product.split('-')
    return ccy_class(ccy_class.FromStrNativeCurrency(base), ccy_class.FromStrNativeCurrency(quote))

  setattr(product_class, 'FromStrNativeProductImpl', FromStrNativeProductImpl)
  return product_class
